//
//  MeViewController.swift
//  BUYOU
//
//  Created by tyuu on 2024/5/29.
//

import UIKit
import SafariServices

class MeViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
    
    let logoImageView = UIImageView()
    
    let settingsOptions = ["当前版本", "清除缓存", "隐私政策", "联系我们"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        navigationItem.title = "我的"
        setupViews()
        setupConstraints()
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
    
    // 设置视图
    private func setupViews() {
        logoImageView.image = UIImage(named: "bpb")  // 确保你有一个名为 "logo" 的图片资源
        logoImageView.layerCornerRadius = 10
        logoImageView.contentMode = .scaleAspectFit
        
        view.addSubview(logoImageView)
        view.addSubview(tableView)
    }
    
    // 设置约束
    private func setupConstraints() {
        logoImageView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide).offset(20)
            make.centerX.equalTo(view)
            make.width.height.equalTo(100)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(logoImageView.snp.bottom).offset(20)
            make.left.right.bottom.equalTo(view)
        }
    }
    
    // UITableViewDataSource 方法
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settingsOptions.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = settingsOptions[indexPath.row]
        return cell
    }
    
    // UITableViewDelegate 方法
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        switch indexPath.row {
        case 0:
            showCurrentVersion()
        case 1:
            clearCache()
        case 2:
            showPrivacyPolicy()
        case 3:
            contactUs()
        default:
            break
        }
    }
    
    // 显示当前版本
    private func showCurrentVersion() {
        let alert = UIAlertController(title: "当前版本", message: "版本号: 1.0.0", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default))
        present(alert, animated: true, completion: nil)
    }
    
    // 清除缓存
    private func clearCache() {
        // 实现清除缓存的逻辑
        let alert = UIAlertController(title: "清除缓存", message: "缓存已清除", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default))
        present(alert, animated: true, completion: nil)
    }
    
    // 显示隐私政策
    private func showPrivacyPolicy() {
  
        if let url = URL(string: "https://docs.qq.com/doc/DUnNYcEZ1WEZVb0Rx") {
              let safariVC = SFSafariViewController(url: url)
              present(safariVC, animated: true, completion: nil)
          }
    }
    
    // 联系我们
    private func contactUs() {
        let alert = UIAlertController(title: "联系我们", message: "邮箱: TonKieuMoi791@gmail.com", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default))
        present(alert, animated: true, completion: nil)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
